import re
from PIL import Image
from django.db.models import Max
from .models import Question

# 定义题干匹配的正则表达式
pattern_question_title = r'^\.\?\d+\s+'
# 定义选项匹配的正则表达式
pattern_question_options = r'^[A-Z]\.'

def get_subject_pinyin(subject_id):
    # 假设科目名称可以通过subject_id从其他表或字典中获取
    # 这里简单返回subject_id的字符串形式
    return str(subject_id)

def determine_question_type(option_str, answer):
    if not option_str.strip():  # 如果选项为空，则为判断题
        return 'judge'
    elif len(answer) == 1:  # 如果答案只有一个，则为单选题
        return 'single'
    else:  # 否则为多选题
        return 'multiple'

def get_max_id():
    # 使用aggregate方法获取最大id
    max_id = Question.objects.aggregate(max_id=Max('id'))['max_id']

    # 如果表中没有数据，max_id可能是None
    if max_id is None:
        max_id = 0  # 或者其他默认值
    return max_id + 1

def combine_images_horizontally(image_paths, output_path):
    images = [Image.open(path) for path in image_paths]
    widths, heights = zip(*(img.size for img in images))

    total_width = sum(widths)
    max_height = max(heights)

    combined_image = Image.new('RGB', (total_width, max_height))

    x_offset = 0
    for img in images:
        combined_image.paste(img, (x_offset, 0))
        x_offset += img.size[0]

    combined_image.save(output_path)
    return output_path